package com.shiliuzi.www.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String RABBIT_EXCHANGE="RABBIT_EXCHANGE";
    public static final String DEAD_QUEUE="DEAD_QUEUE";
    public static final String DEAD_LETTER_QUEUE="DEAD_LETTER_QUEUE";
    public static final String DEAD_EXCHANGE="DEAD_EXCHANGE";

    public static final String EMAIL_QUEUE="EMAIL_QUEUE";
    public static final String EMAIL_EXCHANGE="EMAIL_EXCHANGE";

    /**
     * 延迟交换机
     */
    @Bean(RABBIT_EXCHANGE)
    public Exchange rabbitExchange() {
        return ExchangeBuilder.
                topicExchange(RABBIT_EXCHANGE).
                durable(true).
                build();
    }

    /**
     * 延迟ttl队列
     */
    @Bean(DEAD_QUEUE)
    public Queue deadTTLQueue() {
        return QueueBuilder.
                durable(DEAD_QUEUE).
                deadLetterExchange(DEAD_EXCHANGE).
                deadLetterRoutingKey("dead.returns").
                ttl(7*24*60*60*1000).
                build();
    }


    /**
     * 死信交换机
     */
    @Bean(DEAD_EXCHANGE)
    public Exchange deadExchange() {
        return ExchangeBuilder
                .topicExchange(DEAD_EXCHANGE)
                .build();
    }

    /**
     * 死信队列
     */
    @Bean(DEAD_LETTER_QUEUE)
    public Queue deadQueue() {
        return QueueBuilder
                .durable(DEAD_LETTER_QUEUE)
                .build();
    }

    /**
     * 处理email队列
     * @return
     */
    @Bean(EMAIL_QUEUE)
    public Queue emailQueue() {
        return QueueBuilder
                .durable(EMAIL_QUEUE)
                .build();
    }

    /**
     * 处理email交换机
     * @return
     */
    @Bean(EMAIL_EXCHANGE)
    public Exchange emailExchange() {
        return ExchangeBuilder
                .topicExchange(EMAIL_EXCHANGE)
                .durable(true)
                .build();
    }

    /**
     * 延迟ttl绑定
     */
    @Bean
    public Binding buildQueueExchangeTTL(@Qualifier(DEAD_QUEUE) Queue queue,
                                         @Qualifier(RABBIT_EXCHANGE) Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("ttl.#")
                .noargs();
    }

    /**
     * 延迟绑定
     */
    @Bean
    public Binding buildQueueExchangeDead(@Qualifier(DEAD_LETTER_QUEUE) Queue queue,
                                          @Qualifier(DEAD_EXCHANGE) Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("dead.#")
                .noargs();
    }

    /**
     * email绑定
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding buildQueueExchangeEmail(@Qualifier(EMAIL_QUEUE) Queue queue,
                                           @Qualifier(EMAIL_EXCHANGE) Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("email.#")
                .noargs();
    }
}
